﻿<#
.SYNOPSIS 
    Retrieves the PeopleSoft Application Server process list.
.DESCRIPTION 
    Displays the processes that have been booted for the specified domain. This includes the system process ID for each process.
    The domain must be booted, otherwise this command will not return any results.
.NOTES 
    File Name  : Get-PsftPrcsList.ps1 
    Author     : Scott Kearney - scott@kearney.tk 
    Requires   : PowerShell V3 
.EXAMPLE 
    Get-PsftPrcsList -PS_HOME C:\psoft\PS_HOME -PS_CFG_HOME C:\psoft\PS_CFG_HOME -ServerType AppServer -Domain DOMAIN1
    Displays the system process IDs for the domain named DOMAIN1.
.PARAMETER PS_HOME
    The full path to the PS_HOME directory for this domain.
.PARAMETER PS_CFG_HOME
    The full path to the PS_CFG_HOME directory for this domain.
.PARAMETER ServerType
    The type of server on which to perform the action.
.PARAMETER Domain
    The name of the domain on which to perform the action.
#> 
function Get-PsftPrcsList
{
    [CmdletBinding(SupportsShouldProcess=$True,ConfirmImpact='Low')]
    [OutputType([Array])]
    Param
    (
        # The PS_HOME directory.
        # See http://docs.oracle.com/cd/E41633_01/pt853pbh1/eng/pt/tsvt/concept_UnderstandingPS_HOMEandPS_CFG_HOME-eb7ece.html
        [Parameter(Mandatory=$true,
                   ValueFromPipelineByPropertyName=$true,
                   HelpMessage = "The full path to the PS_HOME directory for this domain.",
                   Position=0)]
        [ValidateScript({Validate_PS_HOME $_})]
        [String]
        $PS_HOME,

        # The PS_CFG_HOME directory.
        # See http://docs.oracle.com/cd/E41633_01/pt853pbh1/eng/pt/tsvt/concept_UnderstandingPS_HOMEandPS_CFG_HOME-eb7ece.html
        [Parameter(Mandatory=$true,
                   ValueFromPipelineByPropertyName=$true,
                   HelpMessage = "The full path to the PS_CFG_HOME directory for this domain.",
                   Position=1)]
        [ValidateScript({Validate_PS_CFG_HOME $_})]
        [String]
        $PS_CFG_HOME,

        # The type of server on which to perform the action.
        [Parameter(Mandatory=$true,
                   ValueFromPipelineByPropertyName=$true,
                   HelpMessage = "The type of server on which to perform the action.",
                   Position=2)]
        [ValidateSet("AppServer","PrcsScheduler")]
        $ServerType,

        # The name of the domain on which to perform the action.
        # Should be a directory under the PS_CFG_HOME
        [Parameter(Mandatory=$true,
                   ValueFromPipelineByPropertyName=$true,
                   HelpMessage = "The name of the domain on which to perform the action.",
                   Position=3)]
        [ValidateScript({Validate_Domain $PS_CFG_HOME $ServerType $_})]
        [String]
        $Domain
    )

    Begin
    {
    }
    Process
    {
        # Set up variables
        $PSAdmin = $null

        # Displays the processes that have been booted for the specified domain.
        $command = "pslist"

        # Invoke PSAdmin to display the list of processes
        $PSAdmin = Invoke-PSAdmin -PS_HOME $PS_HOME -PS_CFG_HOME $PS_CFG_HOME -ServerType $ServerType -Domain $Domain -Command $command
		
		$result = $PSAdmin.Result
        
        $return = @()

        # Parse the results of PSAdmin and split them into members of the object.
        $result | Select -Skip 2 | %{
                $return += New-Object Object |
                    Add-Member NoteProperty PID           ((   [int]$_.Substring(0,5))  -replace " ") -PassThru | 
                    Add-Member NoteProperty PPID          ((   [int]$_.Substring(9,5)) -replace " ") -PassThru |
                    Add-Member NoteProperty Leader        (([String]$_.Substring(18,7)) -replace " ") -PassThru | 
                    Add-Member NoteProperty ProcessName   (([String]$_.Substring(27))  -replace " ") -PassThru
            }

        # Return the results of the command, if any
        return $return
    }
    End
    {
    }
}